package com.nf.health.app.myfamily.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.my.HouseHealthyManagerProActivity;
import com.nf.health.app.activity.my.HousePersionKnowledgeRulerActivity;
import com.nf.health.app.activity.my.bean.HealthyOldManManagerProject;
import com.nf.health.app.activity.my.historydata.EcgQueryHIstoryActivity;
import com.nf.health.app.activity.my.historydata.SweatQueryHistoryActivity2;
import com.nf.health.app.activity.my.impl.MyContact;
import com.nf.health.app.adapter.MemberAdapter;
import com.nf.health.app.customview.CircleProgress;
import com.nf.health.app.http.Act;
import com.nf.health.app.models.DeviceData;
import com.nf.health.app.models.Family;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/** 
 * @ClassName: FamilyMemberActivity 
 * @Description:家庭成员数据
 * @author Ian 
 * @date 2015-11-4 上午9:50:51 
 *  
 */
public class FamilyMemberActivity extends BaseActivity {

	private ListView listview;
	private CircleProgress cicleProgress;
	private List<DeviceData> datas;
	private List<DeviceData> datas1;
	private Family family;
	private LinearLayout ll_top_score_circle;
	DeviceData data;

	private ImageView titlebar_right_view;
	private MemberAdapter memberAdapter;
	int total;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		family = (Family) getIntent().getSerializableExtra("member");
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_family_member);
		if(family == null){
			return;
		}
		
		ViewUtil.setText(this, R.id.titlebar_title_tv, "加载中...");
		listview = ViewUtil.getView(this, R.id.listview);

		titlebar_right_view = (ImageView) findViewById(R.id.titlebar_right_view);
		ll_top_score_circle = (LinearLayout) findViewById(R.id.ll_top_score_circle);
		cicleProgress = ViewUtil.getView(this, R.id.cicleProgress);
		cicleProgress.setHealthState(family.getHealth());
		if(family.getType().equals("oldMobile")||family.getType().equals("family")){
			titlebar_right_view.setImageResource(R.drawable.ic_title_setting);
			titlebar_right_view.setVisibility(View.VISIBLE);
		}

		if(TextUtils.isEmpty(family.getScore())){
			total = 0;
		}else{
			total = (int) Float.parseFloat(family.getScore());
		}
		if(!TextUtils.isEmpty(getIntent().getStringExtra("Score")) && getIntent().getStringExtra("Score").equals("gone")){//从家庭责任人过来的时候隐藏 分数进度条
			ll_top_score_circle.setVisibility(View.GONE);
		}else{
			ll_top_score_circle.setVisibility(View.VISIBLE);
			new Thread(new Runnable() {

				@Override
				public void run() {
					SystemClock.sleep(500);
					int progress = 0;
					while (progress <= total) {
						cicleProgress.setProgress(progress,total);
						progress++;
						SystemClock.sleep(30);
					}
				}
			}).start();
		}


		provider.requestGetFamilyMemberData(family.getTouserid(), "getMemberdata");

		listview.setOnItemClickListener(new OnItemClickListener() {//家人数据点击事件

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				DeviceData deviceData =  datas1.get(position);
				if(deviceData.getDeviceType().equals(MyContact.DEVICE_TYPE_SHOW_OLD_FLAG)){//老年人健康管理方案
					Bundle bundlePrivacy = new Bundle();
					bundlePrivacy.putSerializable(MyContact.URL, deviceData.getWebViewUrl());
					ActivityUtils.showActivity(FamilyMemberActivity.this, HouseHealthyManagerProActivity.class, bundlePrivacy);
				}else  if(deviceData.getDeviceType().equals(MyContact.DEVICE_TYPE_SICK_FLAG)){//慢性疾病管理方案
					Bundle bundlePrivacy = new Bundle();
					bundlePrivacy.putSerializable(MyContact.URL, deviceData.getWebViewUrl());
					ActivityUtils.showActivity(FamilyMemberActivity.this, HouseHealthyManagerProActivity.class, bundlePrivacy);
				}else{
					if(family.getType().equals("oldMobile")&&position == 2){//跳转到足迹页
						Bundle bundle = new Bundle();
						bundle.putString("imei",family.getAccount());
						ActivityUtils.showActivity(FamilyMemberActivity.this, OldPersonalMapActivity.class,bundle);

					}else if(family.getType().equals("oldMobile")&&DeviceData.Type.BLOOD_SUGAR.equals(datas1.get(position).getDeviceType())){
						Bundle bundle = new Bundle();
						bundle.putSerializable("member",family);
						bundle.putString("deviceName",datas1.get(position).getDeviceName());
						bundle.putString("deviceType",datas1.get(position).getDeviceType());
						ActivityUtils.showActivity(FamilyMemberActivity.this, DataDetails2Activity.class, bundle);
					}else if(family.getType().equals("oldMobile")&&DeviceData.Type.BLOOD_PRESSURE.equals(datas1.get(position).getDeviceName())){
						Bundle bundle = new Bundle();
						bundle.putSerializable("member",family);
						bundle.putString("deviceName",datas1.get(position).getDeviceName());
						bundle.putString("deviceType",datas1.get(position).getDeviceType());
						ActivityUtils.showActivity(FamilyMemberActivity.this, DataDetails2Activity.class, bundle);
					}else if(family.getType().equals(MyContact.DEVICE_TYPE_SHOW_OLD_FLAG)){//老年人健康管理方案


					}else if(family.getType().equals(MyContact.DEVICE_TYPE_SHOW_OLD_FLAG)){//慢性疾病管理方案

						Bundle bundlePrivacy = new Bundle();
						bundlePrivacy.putSerializable(MyContact.URL, deviceData.getWebViewUrl());
						ActivityUtils.showActivity(FamilyMemberActivity.this, HousePersionKnowledgeRulerActivity.class, bundlePrivacy);
					}else {
						String type = datas.get(position).getDeviceType();
						Bundle bundle = new Bundle();
						bundle.putSerializable("member", family);
						bundle.putString("deviceName", datas.get(position).getDeviceName());
						bundle.putString("deviceType", datas.get(position).getDeviceType());
						if(type.equals(DeviceData.Type.ECG)){//心电
							ActivityUtils.showActivity(FamilyMemberActivity.this, EcgQueryHIstoryActivity.class, bundle);
						}else if(type.equals(DeviceData.Type.SWEAT)){//汗液
							ActivityUtils.showActivity(FamilyMemberActivity.this, SweatQueryHistoryActivity2.class, bundle);
						}else{
							ActivityUtils.showActivity(FamilyMemberActivity.this, DataDetails2Activity.class, bundle);
						}
					}
				}
			}
		});
		titlebar_right_view.setOnClickListener(new OnClickListener() {//老人机设置

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(family.getType().equals("oldMobile")){
					Intent intent = new Intent(FamilyMemberActivity.this,OldPersonalSettingActivity.class);
					intent.putExtra("imei",family.getAccount());
					intent.putExtra("phone",family.getCellphone());
					intent.putExtra("touserid",family.getTouserid());
					startActivity(intent);
					finish();
				}else{
					Intent intent = new Intent(FamilyMemberActivity.this,OldSettongNameActivity.class);
					intent.putExtra("imei",family.getAccount());
					intent.putExtra("phone",family.getCellphone());
					intent.putExtra("touserid",family.getTouserid());
					intent.putExtra("type","family");
					startActivity(intent);
					finish();
					
				}

			}
		});
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		switch (actionName){
			case  "getMemberdata":
				String title = TextUtils.isEmpty(family.getNick())?family.getAccount():family.getNick();
				ViewUtil.setText(this, R.id.titlebar_title_tv, "“"+title+"”的数据");
				datas = (List<DeviceData>) object;

				if(family.getType().equals("oldMobile")){

					datas1 = new ArrayList<DeviceData>();

					for(int i = 0;i<datas.size();i++){
						if(datas.get(i).getDeviceName().equals("血糖数据")){
							datas1.add(datas.get(i));
						}else if(datas.get(i).getDeviceName().equals("血压数据")){
							datas1.add(datas.get(i));

						}
					}

					datas1.add(new DeviceData(null,"","","轨迹数据", "", "oldMobile"));
					if(memberAdapter == null){
						memberAdapter = new MemberAdapter(this, datas1,family.getType());
						listview.setAdapter(memberAdapter);
					}else{
						memberAdapter.notifyMyDeviceDataChanged(datas1,family.getType());
					}
				}else{
					datas1 = datas;
					if(memberAdapter == null){
						memberAdapter = new MemberAdapter(this, datas1,family.getType());
						listview.setAdapter(memberAdapter);
					}else{
						memberAdapter.notifyMyDeviceDataChanged(datas1,family.getType());
					}
				}
				getPersonHealthData();
				break;
			case Act.Flag.FLAG_GET_DUTYPERSONHEALTH://健康改善方案
				HealthyOldManManagerProject healthyOldManManagerProject = (HealthyOldManManagerProject) object;
				if(!TextUtils.isEmpty(healthyOldManManagerProject.getOldFlag())){  //老年人显示标识	string	0不显示 1显示
					if(healthyOldManManagerProject.getOldFlag().equals("1")){
						DeviceData deviceData = new DeviceData();
						deviceData.setDeviceName("老年人健康改善方案");
						deviceData.setDeviceType(MyContact.DEVICE_TYPE_SHOW_OLD_FLAG);
						deviceData.setWebViewUrl(healthyOldManManagerProject.getOldUrl());
						datas.add(deviceData);
					}

				}
				if(!TextUtils.isEmpty(healthyOldManManagerProject.getSlowFlag())){ //慢性病显示标识	string	0不显示 1显示
					if(healthyOldManManagerProject.getSlowFlag().equals("1")){
						DeviceData deviceData = new DeviceData();
						deviceData.setDeviceName("慢性病健康改善方案");
						deviceData.setDeviceType(MyContact.DEVICE_TYPE_SICK_FLAG);
						deviceData.setWebViewUrl(healthyOldManManagerProject.getSlowUrl());
						datas.add(deviceData);
					}
				}
				if(memberAdapter == null){
					memberAdapter = new MemberAdapter(this, datas1,family.getType());
					listview.setAdapter(memberAdapter);
				}else{
					memberAdapter.notifyMyDeviceDataChanged(datas1,family.getType());
				}
				break;
		}

	}

	@Override
	public void handleActionError(String actionName, String response, VolleyError error) {
		super.handleActionError(actionName, response, error);
		switch (actionName) {
			case "getMemberdata":
				getPersonHealthData();
				break;
			case Act.Flag.FLAG_GET_DUTYPERSONHEALTH:

				break;
		}
	}

	/**
	 * 用户（健康明白人）数据
	 */
	private void  getPersonHealthData(){
		if(  !TextUtils.isEmpty(getIntent().getStringExtra(MyContact.FLAG_HELATHY_MANAGER_PROGECT)) && getIntent().getStringExtra(MyContact.FLAG_HELATHY_MANAGER_PROGECT).equals(MyContact.FLAG_HELATHY_MANAGER_PROGECT)){
			provider.getPersonHealthData(family.getTouserid());
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
